package cn.kgc.service;

import cn.kgc.client.RestDmLinkUserClient;
import cn.kgc.client.RestDmOrderClient;
import cn.kgc.client.RestDmOrderLinkUserClient;
import cn.kgc.client.RestDmSchedulerSeatClient;
import cn.kgc.pojo.DmOrderLinkUser;
import cn.kgc.pojo.DmSchedulerSeat;
import cn.kgc.utils.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceConsumer {

    @Autowired
    RestDmSchedulerSeatClient restDmSchedulerSeatClient;

    @Autowired
    RestDmOrderClient restDmOrderClient;

    @Autowired
    RestDmOrderLinkUserClient restDmOrderLinkUserClient;

    @RabbitListener(queues= Constants.RabbitQueueName.TO_RESET_LINKUSER_QUQUE)
    public void resetLinkedUser(Integer orderId, Message message, Channel channel) throws IOException {
            try {
                Map map =new HashMap();
                map.put("orderId",orderId);
                List<DmOrderLinkUser> dmLinkUserListByMap = restDmOrderLinkUserClient.getDmOrderLinkUserListByMap(map);
                for (DmOrderLinkUser dmOrderLinkUser : dmLinkUserListByMap) {
                    restDmOrderLinkUserClient.qdtxDelDmOrderLinkUser(dmOrderLinkUser.getId());
                }
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (Exception e) {
                e.printStackTrace();
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            }
      }


    @RabbitListener(queues= Constants.RabbitQueueName.TO_RESET_SEAT_QUQUE)
    public void resetSeatMsg(Map map, Message message, Channel channel) throws IOException {
        java.util.List<DmSchedulerSeat>  seats = (List<DmSchedulerSeat>) map.get("seats");
          for (DmSchedulerSeat bookSeat : seats) {
                bookSeat.setStatus(Constants.SchedulerSeatStatus.SchedulerSeat_FREE);
                bookSeat.setOrderNo("");
              try {
                  restDmSchedulerSeatClient.qdtxModifyDmSchedulerSeat(bookSeat);
                  channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
              } catch (Exception e) {
                  e.printStackTrace();
                  channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
              }
          }
    }


    @RabbitListener(queues= Constants.RabbitQueueName.TO_DEL_ORDER_QUQUE)
    public void DelOrder(Integer orderId, Message message, Channel channel) throws IOException {

            try {
                restDmOrderClient.qdtxDelDmOrder(new Long(orderId));
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (Exception e) {
                e.printStackTrace();
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            }
        }


    @RabbitListener(queues=Constants.RabbitQueueName.TO_DEAD_OVER_TIME_QUEUE)
        public void CheckOrderOverTime(String orderNo,Message message, Channel channel){
            //1.根据orderNo获取订单信息
            //2.查看这个订单是否已经付款
            //3.没有付款则订单状态改成超时状态
            //4.订单座位状态重置

        }
}
